﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using Microsoft.Phone.Tasks;
using YellowMap.Resources;

namespace YellowMap
{
    public partial class DetailDialog : UserControl
    {
        public event Action<string, string> OnSearchDirection;
        public event Action OnSaveFavorite;
        public bool IsRemainInFavorites = false;
        public string str_current_location = "str_current_location";
        public string str_this_venue = "str_this_venue";

        public bool IsShow { get; set; }

        public Detail Detail { get; set; }
        Place _place = null;
        public Place Place
        {
            get
            {
                return _place;
            }

            set
            {
                _place = value;
                /*
                string sCate = value.SubCate.Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries)[0];
                var category = AppViewModel.Instance.Categories.Where(c => c.Id == sCate).First();
                string icon = category.Icon;
                string color = category.Color;
                rectangle.Fill = new SolidColorBrush(parseColor(color));
                image.Source = new BitmapImage(new Uri(icon, UriKind.Relative));
                 * */
                
            }
        }

        public DetailDialog()
        {
            IsShow = false;
            InitializeComponent();
            this.Visibility = System.Windows.Visibility.Collapsed;
            VisualStateManager.GoToState(this, "hide", false);
            //ReloadLanguage(AppViewModel.Instance.SettingVM.Language);
            ChangeThemes();
            //tbFrom.Text = str_current_location;
            //tbTo.Text = str_this_venue;
        }

        private void ChangeThemes()
        {
            canvasDetail.Background = new SolidColorBrush(AppViewModel.Instance.SettingVM.BGColor_screen);
            //canvasDirection.Background = new SolidColorBrush(AppViewModel.Instance.SettingVM.BGColor_screen);
            txtname.Foreground = new SolidColorBrush(AppViewModel.Instance.SettingVM.BGColor_Title);
            txtAddress.Foreground = new SolidColorBrush(AppViewModel.Instance.SettingVM.BGColor_Text);
            //txtFrom.Foreground = new SolidColorBrush(AppViewModel.Instance.SettingVM.BGColor_Text);
            //txtTo.Foreground = new SolidColorBrush(AppViewModel.Instance.SettingVM.BGColor_Text);
            //txtSearch.Foreground = new SolidColorBrush(AppViewModel.Instance.SettingVM.BGColor_Text);
            //btnSearch.Foreground = new SolidColorBrush(AppViewModel.Instance.SettingVM.BGColor_Text);
            //btnClose.Foreground = new SolidColorBrush(AppViewModel.Instance.SettingVM.BGColor_Text);
            abc.Fill = new SolidColorBrush(AppViewModel.Instance.SettingVM.BGColor_screen);
            if ((Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"] != Visibility.Visible)
            {
                imgFavorite.Source = new BitmapImage(new Uri("/Resources/NewIcon/favorite_black.png", UriKind.Relative));
                imgDirection.Source = new BitmapImage(new Uri("/Resources/NewIcon/directions_black.png", UriKind.Relative));
                imgCall.Source = new BitmapImage(new Uri("/Resources/NewIcon/phone_black.png", UriKind.Relative));
            }
            else
            {
                imgFavorite.Source = new BitmapImage(new Uri("/Resources/NewIcon/favorite_white.png", UriKind.Relative));
                imgDirection.Source = new BitmapImage(new Uri("/Resources/NewIcon/directions_white.png", UriKind.Relative));
                imgCall.Source = new BitmapImage(new Uri("/Resources/NewIcon/phone_white.png", UriKind.Relative));
            }
        }

        public void Hide()
        {
            VisualStateManager.GoToState(this, "hide", true);
            sbInvisible.Begin();
            IsShow = false;
        }

        public void Show(Detail d = null)
        {

            //ReloadLanguage(AppViewModel.Instance.SettingVM.Language);
            if (AppViewModel.Instance.SettingVM.Language == "Vi")
            {
                str_current_location = LangVi.str_current_location;
            }
            else
            {
                str_current_location = LangEn.str_current_location;
            }
            str_this_venue = d.Name;
            canvasDetail.Visibility = System.Windows.Visibility.Visible;
            //canvasDirection.Visibility = System.Windows.Visibility.Collapsed;

            if (d != null)
            {
                txtAddress.Text = d.Address;
                txtname.Text = d.Name;

                Detail = d;
            }

            txtAddress.Text = Place.Address;
            this.Visibility = System.Windows.Visibility.Visible;
            VisualStateManager.GoToState(this, "show", true);
            IsShow = true;
            checkFavoritesItem();
            //image.Source = new BitmapImage(new Uri(d.Image, UriKind.Absolute));
            
            //if (MainPage.isSearchByCate == false)
            //{
            //    string icon = "";
            //    image.Source = new BitmapImage(new Uri(icon, UriKind.Relative));
            //}
            //else
            if (Place.SubCate != null && Place.SubCate.Length != 0)
            {
                string icon = "/Resources/SubCateIcon/" + Place.SubCate + ".png";
                image.Source = new BitmapImage(new Uri(icon, UriKind.Relative));
            }
            else
            {
                string icon = "";
                image.Source = new BitmapImage(new Uri(icon, UriKind.Relative));
            }
        }
        //private void ReloadLanguage(string languageName)
        //{
        //    switch (languageName)
        //    {
        //        case "Vi":
        //            tbFrom.Text = LangVi.str_current_location;
        //            txtFrom.Text = LangVi.str_from;
        //            txtTo.Text = LangVi.str_to;
        //            btnClose.Content = LangVi.str_btnCancel;
        //            btnSearch.Content = LangVi.str_btnSearch;
        //            txtSearch.Text = LangVi.str_titSearchDirection;
        //            break;
        //        case "En":
        //            txtFrom.Text = LangEn.str_from;
        //            txtTo.Text = LangEn.str_to;
        //            btnClose.Content = LangEn.str_btnCancel;
        //            btnSearch.Content = LangEn.str_btnSearch;
        //            txtSearch.Text = LangEn.str_titSearchDirection;
        //            break;
        //    }
        //}
        private void LayoutRoot_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            // TODO: Add event handler implementation here.

        }

        private void outSide_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            // TODO: Add event handler implementation here.
            Hide();
        }

        private void btnfavorite_Click(object sender, System.Windows.RoutedEventArgs e)
        {

        }

       

        //private void tbTo_LostFocus(object sender, System.Windows.RoutedEventArgs e)
        //{
        //    tbTo.Text = tbTo.Text.Trim();
        //    if (tbTo.Text == "")
        //    {
        //        tbTo.Text = str_this_venue;
        //        tbTo.Foreground = new SolidColorBrush(Color.FromArgb(255, 100, 100, 100));
        //    }
        //}

        //private void tbTo_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        //{
        //    if (tbTo.Text == str_this_venue)
        //    {
        //        tbTo.Text = "";
        //        tbTo.Foreground = new SolidColorBrush(Colors.Black);
        //    }
        //}

        //private void tbFrom_LostFocus(object sender, System.Windows.RoutedEventArgs e)
        //{
        //    tbFrom.Text = tbFrom.Text.Trim();
        //    if (tbFrom.Text == "")
        //    {
        //        tbFrom.Text = str_current_location;
        //        tbFrom.Foreground = new SolidColorBrush(Color.FromArgb(255, 100, 100, 100));
        //    }
        //}

        //private void tbFrom_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        //{
        //    if (tbFrom.Text == str_current_location)
        //    {
        //        tbFrom.Text = "";
        //        tbFrom.Foreground = new SolidColorBrush(Colors.Black);
        //    }
        //}

        //private void btnClose_Click(object sender, System.Windows.RoutedEventArgs e)
        //{
        //    // TODO: Add event handler implementation here.
        //    canvasDetail.Visibility = System.Windows.Visibility.Visible;
        //    canvasDirection.Visibility = System.Windows.Visibility.Collapsed;
        //}

        private void tbFrom_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            // TODO: Add event handler implementation here.
            if (e.Key == System.Windows.Input.Key.Enter)
            {
                startSearch();
                e.Handled = false;
            }
        }

        private void tbTo_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            // TODO: Add event handler implementation here.
            if (e.Key == System.Windows.Input.Key.Enter)
            {
                startSearch();

                e.Handled = false;
            }
        }

        void startSearch()
        {
            //tbFrom.Text = tbFrom.Text.Trim();
            //tbTo.Text = tbTo.Text.Trim();
            //string from = "";
            //string to = "";
            
            //if (tbFrom.Text != str_current_location)
            //{
            //    from = tbFrom.Text.Trim();
            //}

            //if (tbTo.Text != str_this_venue)
            //{
            //    to = tbTo.Text.Trim();
            //}
            string from = "";
            string to = "";
            OnSearchDirection(from, to);
            Hide();
        }


        static Color parseColor(string hexaColor)
        {
            hexaColor = hexaColor.Trim(new char[] { '#' });
            if (hexaColor.Length == 6)
            {
                hexaColor += "ff";
            }

            return Color.FromArgb(
                    Convert.ToByte(hexaColor.Substring(6, 2), 16),
                    Convert.ToByte(hexaColor.Substring(0, 2), 16),
                    Convert.ToByte(hexaColor.Substring(2, 2), 16),
                    Convert.ToByte(hexaColor.Substring(4, 2), 16));
        }

        private void imgDirection_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            
            //(MainPage.Instance.ApplicationBar.Buttons[3] as ApplicationBarIconButton).IsEnabled = false;
            if (MainPage.Instance.tbSearch.Visibility == System.Windows.Visibility.Visible)
            {
                MainPage.Instance.tbSearch.Visibility = System.Windows.Visibility.Collapsed;
                MainPage.Instance.titleBG.Visibility = System.Windows.Visibility.Collapsed;
                MainPage.Instance.imagesearch.Visibility = System.Windows.Visibility.Collapsed;
            }
            startSearch();
            // TODO: Add event handler implementation here.
            //tbFrom.Text = str_current_location;
            //tbTo.Text = str_this_venue;
            //tbFrom.Foreground = new SolidColorBrush(Color.FromArgb(255, 100, 100, 100));
            //tbTo.Foreground = new SolidColorBrush(Color.FromArgb(255, 100, 100, 100));
            //canvasDetail.Visibility = System.Windows.Visibility.Collapsed;
            //canvasDirection.Visibility = System.Windows.Visibility.Visible;
        }

        //private void btnDirection_Click(object sender, System.Windows.RoutedEventArgs e)
        //{

        //    ReloadLanguage(AppViewModel.Instance.SettingVM.Language);
        //    tbFrom.Text = str_current_location;
        //    tbTo.Text = str_this_venue;
        //    tbFrom.Foreground = new SolidColorBrush(Color.FromArgb(255, 100, 100, 100));
        //    tbTo.Foreground = new SolidColorBrush(Color.FromArgb(255, 100, 100, 100));
        //    canvasDetail.Visibility = System.Windows.Visibility.Collapsed;
        //    canvasDirection.Visibility = System.Windows.Visibility.Visible;
        //}

        private async void checkFavoritesItem()
        {

            if (await DBHelper.CheckFavorite(Detail))
            {
                IsRemainInFavorites = true;
                return;
            }
            else
                IsRemainInFavorites = false;

        }
        private void imgFavorite_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            // TODO: Add event handler implementation here.
            if (IsRemainInFavorites == false)
            {
                OnSaveFavorite();
                IsRemainInFavorites = true;
            }
            else
            {
                if (AppViewModel.Instance.SettingVM.Language == "Vi")
                {
                    MainPage.Instance.Toast(LangVi.addedFavorites);
                }
                else
                {
                    MainPage.Instance.Toast(LangEn.addedFavorites);
                }
                return;
            }
        }

        private void imgCall_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            // TODO: Add event handler implementation here.
            if (Detail.FormatedPhoneNumber == null)
            {
                if (AppViewModel.Instance.SettingVM.Language == "Vi")
                {
                    MainPage.Instance.Toast(LangVi.NoPhoneNumber);
                }
                else
                {
                    MainPage.Instance.Toast(LangEn.NoPhoneNumber);
                }
                return;
            }
            else
            {
                PhoneCaller(Detail.Name, Detail.FormatedPhoneNumber);
            }
        }

        public static void PhoneCaller(string name, string phoneNumber)
        {

            PhoneCallTask task = new PhoneCallTask();
            task.PhoneNumber = phoneNumber;
            task.DisplayName = name;
            task.Show();
        }

        private void imgFavorite_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            imgFavorite_bg.Visibility = System.Windows.Visibility.Visible;
            if ((Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"] == Visibility.Visible)
                imgFavorite.Source = new BitmapImage(new Uri("/Resources/NewIcon/favorite_black.png", UriKind.Relative));
            else
                imgFavorite.Source = new BitmapImage(new Uri("/Resources/NewIcon/favorite_white.png", UriKind.Relative));
        }

        private void imgFavorite_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            imgFavorite_bg.Visibility = System.Windows.Visibility.Collapsed;
            if ((Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"] == Visibility.Visible)
                imgFavorite.Source = new BitmapImage(new Uri("/Resources/NewIcon/favorite_white.png", UriKind.Relative));
            else
                imgFavorite.Source = new BitmapImage(new Uri("/Resources/NewIcon/favorite_black.png", UriKind.Relative));
        }

        private void imgFavorite_Hold(object sender, System.Windows.Input.GestureEventArgs e)
        {
            imgFavorite_bg.Visibility = System.Windows.Visibility.Visible;
            if ((Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"] == Visibility.Visible)
                imgFavorite.Source = new BitmapImage(new Uri("/Resources/NewIcon/favorite_black.png", UriKind.Relative));
            else
                imgFavorite.Source = new BitmapImage(new Uri("/Resources/NewIcon/favorite_white.png", UriKind.Relative));
        }

        private void imgDirection_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            imgDirection_bg.Visibility = System.Windows.Visibility.Visible;
            if ((Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"] == Visibility.Visible)
                imgDirection.Source = new BitmapImage(new Uri("/Resources/NewIcon/directions_black.png", UriKind.Relative));
            else
                imgDirection.Source = new BitmapImage(new Uri("/Resources/NewIcon/directions_white.png", UriKind.Relative));
        }

        private void imgDirection_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            imgDirection_bg.Visibility = System.Windows.Visibility.Collapsed;
            if ((Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"] == Visibility.Visible)
                imgDirection.Source = new BitmapImage(new Uri("/Resources/NewIcon/directions_white.png", UriKind.Relative));
            else
                imgDirection.Source = new BitmapImage(new Uri("/Resources/NewIcon/directions_black.png", UriKind.Relative));
        }

        private void imgDirection_Hold(object sender, System.Windows.Input.GestureEventArgs e)
        {
            imgDirection_bg.Visibility = System.Windows.Visibility.Visible;
            if ((Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"] == Visibility.Visible)
                imgDirection.Source = new BitmapImage(new Uri("/Resources/NewIcon/directions_black.png", UriKind.Relative));
            else
                imgDirection.Source = new BitmapImage(new Uri("/Resources/NewIcon/directions_white.png", UriKind.Relative));
        }

        private void imgCall_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            imgCall_bg.Visibility = System.Windows.Visibility.Visible;
            if ((Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"] == Visibility.Visible)
                imgCall.Source = new BitmapImage(new Uri("/Resources/NewIcon/phone_black.png", UriKind.Relative));
            else
                imgCall.Source = new BitmapImage(new Uri("/Resources/NewIcon/phone_white.png", UriKind.Relative));
        }

        private void imgCall_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            imgCall_bg.Visibility = System.Windows.Visibility.Collapsed;
            if ((Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"] == Visibility.Visible)
                imgCall.Source = new BitmapImage(new Uri("/Resources/NewIcon/phone_white.png", UriKind.Relative));
            else
                imgCall.Source = new BitmapImage(new Uri("/Resources/NewIcon/phone_black.png", UriKind.Relative));
        }

        private void imgCall_Hold(object sender, System.Windows.Input.GestureEventArgs e)
        {
            imgCall_bg.Visibility = System.Windows.Visibility.Visible;
            if ((Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"] == Visibility.Visible)
                imgCall.Source = new BitmapImage(new Uri("/Resources/NewIcon/phone_black.png", UriKind.Relative));
            else
                imgCall.Source = new BitmapImage(new Uri("/Resources/NewIcon/phone_white.png", UriKind.Relative));
        }
    }
}
